import random

li1 = [['牛奶', 5], ['衣服', 200], ['家具', 500]]
li2 = []  # 购物车
money = 5000

# 随机选择一个商品并生成折扣
D = random.randint(0, 2)  # 随机选择商品索引
d = random.randint(1, 10)  # 随机折扣1-10折

# 创建折扣券字典
discount_coupons = {}
selected_product = li1[D][0]  # 获取选中的商品名称
discount_coupons[selected_product] = d / 10  # 存储折扣率

print(f"今日优惠券: {selected_product} {d}折")

while True:
    # 显示购物车状态
    if li2:
        cart_items = [item[0] for item in li2]
        print(f"\n当前购物车: {cart_items}")

    a = input("请输入你要买的商品 (输入'Q/q'结账, 'R/r'退货, '退出'离开): ")

    # 退出命令判断
    if a.upper() == 'Q':
        if li2:
            # 分别计算打折商品和未打折商品的总价
            discount_total = 0  # 打折商品总价
            normal_total = 0  # 未打折商品总价

            for item in li2:
                product_name = item[0]
                original_price = item[1]

                # 如果商品有折扣券，应用折扣
                if product_name in discount_coupons:
                    discounted_price = original_price * discount_coupons[product_name]
                    discount_total += discounted_price
                    print(
                        f"{product_name}: ¥{original_price} → ¥{discounted_price:.1f} ({int(discount_coupons[product_name] * 10)}折)")
                else:
                    normal_total += original_price
                    print(f"{product_name}: ¥{original_price}")

            # 计算最终总价
            total = discount_total + normal_total

            print(f"\n价格明细:")
            if discount_total > 0:
                print(f"   打折商品: ¥{discount_total:.1f}")
            if normal_total > 0:
                print(f"   原价商品: ¥{normal_total:.1f}")
            print(f"   总计: ¥{total:.2f}")

            if money >= total:
                money -= total
                print("购买成功！")
                li2 = []  # 清空购物车
            else:
                print("余额不足！")
        else:
            print("购物车为空，无法结账！")
        break

    # 退货功能
    elif a.upper() == 'R':
        if not li2:
            print("购物车为空，无法退货！")
            continue

        # 显示购物车内容
        print("\n当前购物车中的商品:")
        for i, item in enumerate(li2, 1):
            product_name = item[0]
            price = item[1]
            if product_name in discount_coupons:
                print(f"{i}. {product_name} - ¥{price} ({int(discount_coupons[product_name] * 10)}折)")
            else:
                print(f"{i}. {product_name} - ¥{price}")

        remove_item = input("请输入要退货的商品名称: ")

        # 查找要退货的商品
        found_to_remove = False
        for i, product in enumerate(li2):
            if product[0] == remove_item:
                li2.pop(i)  # 从购物车中移除
                print(f"已退货 {remove_item}")
                found_to_remove = True
                break

        if not found_to_remove:
            print("购物车中没有该商品！")

    # 商品查找和添加
    else:
        found = False
        for product in li1:
            if product[0] == a:
                # 复制商品信息到购物车
                cart_product = product.copy()
                li2.append(cart_product)

                # 检查是否有折扣券
                if a in discount_coupons:
                    print(f"已添加 {a} (享受{discount_coupons[a] * 10}折优惠)")
                else:
                    print(f"已添加 {a}")
                found = True
                break

        if not found:
            print("商品不存在！")